package com.lry.basic.metrics;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public class CompactTimer implements Timer{

    private final Id id;
    private final Id countId;
    private final Id totalId;
    private final Id maxId;
    private final Id averageId;

    private volatile TimerValue value;
    private final Clock clock;

    public CompactTimer(Id id) {
        this(id, SystemClock.INSTANCE);
    }

    public CompactTimer(Id id, Clock clock) {
        this.id = id;
        this.countId = new Id(id.getName()).withTag(id.getTags())
                .withTag(IdConstants.STATISTIC_KEY, IdConstants.STATISTIC_VALUE_COUNT);
        this.totalId = new Id(id.getName()).withTag(id.getTags())
                .withTag(IdConstants.STATISTIC_KEY, IdConstants.STATISTIC_VALUE_TOTAL);
        this.maxId = new Id(id.getName()).withTag(id.getTags())
                .withTag(IdConstants.STATISTIC_KEY, IdConstants.STATISTIC_VALUE_MAX);
        this.averageId = new Id(id.getName()).withTag(id.getTags())
                .withTag(IdConstants.STATISTIC_KEY, IdConstants.STATISTIC_VALUE_AVERAGE);
        this.value = new TimerValue();
        this.clock = clock;
    }


    @Override
    public Id getId() {
        return id;
    }

    @Override
    public void record(long value, TimeUnit unit) {
        this.value.record(value, unit);
    }

    @Override
    public long count() {
        return this.value.getCount();
    }

    @Override
    public long total() {
        return this.value.getTotal();
    }

    @Override
    public long max() {
        return this.value.getMax();
    }

    @Override
    public double average() {
        return this.value.getAverage();
    }

    @Override
    public Iterable<Measurement> measure() {
        double time = clock.getCurrentMilliseconds();
        TimerValue value = this.value;
        this.value = new TimerValue();

        return Arrays.asList(new Measurement(countId, time, value.getCount()),
                //us变成ms
                new Measurement(totalId, time, value.getTotal() * 0.001),
                new Measurement(maxId, time, value.getMax() * 0.001),
                new Measurement(averageId, time, value.getAverage() * 0.001));
    }
}
